home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
UTILREEN
/
GHSCRIPT.LZH
/
QHOSTSCR.EXE
/
_TEMP_.C
next >
Wrap
C/C++ Source or Header
|
1989-10-28
|
16KB
|
513 lines
#line 1 "gdevx.c"
/* Copyright (C) 1989 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gdevx.c */
/* X Windows driver for GhostScript library */
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <malloc.h>
/* Hack to get around the fact that something in the X library */
/* defines uint and ushort.... */
# define uint _uint
# define ushort _ushort
#include "gx.h" /* for gx_bitmap; includes std.h */
# undef _uint
# undef _ushort
#include "gxdevice.h"
/* Flags for patching around bugs in the X library */
#define use_XPutImage 0
#define use_XSetTile 0
/* Procedures */
int x_open(P1(gx_device *));
int x_close(P1(gx_device *));
int x_map_rgb_color(P4(gx_device *, ushort, ushort, ushort));
int x_map_color_rgb(P3(gx_device *, int, ushort *));
int x_sync(P1(gx_device *));
int x_fill_rectangle(P6(gx_device *, int, int, int, int, int));
int x_tile_rectangle(P8(gx_device *, gx_bitmap *, int, int, int, int, int, int));
int x_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, int, int));
int x_copy_color(P9(gx_device *, byte *, int, int, int, int, int, int, int));
int x_draw_line(P6(gx_device *, int, int, int, int, int));
int x_fill_trapezoid(P8(gx_device *, int, int, int, int, int, int, int));
/* The device descriptor */
static gx_device_procs x_procs = {
x_open,
x_close,
x_map_rgb_color,
x_map_color_rgb,
x_sync,
x_fill_rectangle,
x_tile_rectangle,
x_copy_mono,
x_copy_color,
x_draw_line,
x_fill_trapezoid,
gx_default_tile_trapezoid
};
/* Define the X Windows device */
typedef struct gx_device_X_s {
gx_device_common;
/* An XIMage object for writing bitmap images to the screen */
XImage image;
/* Global X state */
Display *dpy;
Screen *scr;
Visual *vis;
Colormap cmap;
Window win;
GC gc;
/* Structure for dealing with the halftone tile. */
/* Later this will become a multi-element cache. */
struct {
Pixmap pixmap;
Pixmap no_pixmap; /* kludge to get around X bug */
int width, height, raster;
byte *bits;
int bits_size;
} ht;
/* Cache the fill style from the GC */
int fill_style;
#define set_fill_style(style)\
if ( xdev->fill_style != style )\
XSetFillStyle(xdev->dpy, xdev->gc, (xdev->fill_style = style))
/* Map color indices to X pixel values */
unsigned long colors[8];
#define black colors[0]
#define white colors[7]
int back_color, fore_color;
#define set_back_color(color)\
if ( xdev->back_color != color )\
XSetBackground(xdev->dpy, xdev->gc, xdev->colors[xdev->back_color = color])
#define set_fore_color(color)\
if ( xdev->fore_color != color )\
XSetForeground(xdev->dpy, xdev->gc, xdev->colors[xdev->fore_color = color])
} gx_device_X;
/* The instance is public. */
gx_device_X x_device = {
sizeof(gx_device_X),
&x_procs,
640, 350, /* x and y extent */
/* Following parameters are initialized for monochrome */
0, /* has color */
1, /* max r-g-b value */
1, /* bits per color pixel */
/* End of monochrome/color parameters */
1, /* bit-big-endian (for now) */
0, /* connection not initialized */
{ 0, 0, /* width, height */
0, XYBitmap, NULL, /* xoffset, format, data */
LSBFirst, 8, /* byte-order, bitmap-unit */
MSBFirst, 8, 1 /* bitmap-bit-order, bitmap-pad, depth */
}
};
/* Macro for casting gx_device argument */
#define xdev ((gx_device_X *)dev)
/* Macro to validate arguments */
#define check_rect()\
if ( w <= 0 || h <= 0 ) return 0;\
if ( x < 0 || x > xdev->width - w || y < 0 || y > xdev->height - h )\
return -1
#if !use_XPutImage
/* XPutImage doesn't work, do it ourselves. */
# undef XPutImage
void alt_put_image();
# define XPutImage(dpy,win,gc,im,sx,sy,x,y,w,h)\
alt_put_image(dev,dpy,win,gc,im,sx,sy,x,y,w,h)
#endif
int
x_open(dev) register gx_device *dev;
{ XSetWindowAttributes xswa;
int winW = xdev->width, winH = xdev->height;
int winX, winY;
#ifdef gs_DEBUG
if ( gs_debug['X'] )
{ extern int _Xdebug;
_Xdebug = 1;
}
#endif
if ( !(xdev->dpy = XOpenDisplay(NULL)) )
{ printf("Cannot open display\n");
exit(1);
}
xdev->scr = DefaultScreenOfDisplay(xdev->dpy);
xdev->vis = DefaultVisualOfScreen(xdev->scr);
xdev->cmap = DefaultColormapOfScreen(xdev->scr);
winX = (WidthOfScreen(xdev->scr) - winW) >> 1;
winY = (HeightOfScreen(xdev->scr) - winH) >> 1;
xdev->black = BlackPixelOfScreen(xdev->scr);
xdev->white = WhitePixelOfScreen(xdev->scr);
xdev->back_color = xdev->fore_color = -1; /* undefined */
/* Figure out monochrome vs. color */
switch ( xdev->vis->class )
{
case StaticGray:
case GrayScale:
xdev->has_color = 0;
{ int i;
for ( i = 1; i < 7; i++ ) xdev->colors[i] = xdev->white;
}
break;
default: /* color */
xdev->has_color = 1;
/* Just do primary colors for now */
{ XColor xc;
int i;
for ( i = 1; i < 7; i++ )
{ xc.red = (i & 4 ? (ushort)-1 : 0);
xc.green = (i & 2 ? (ushort)-1 : 0);
xc.blue = (i & 1 ? (ushort)-1 : 0);
XAllocColor(xdev->dpy, xdev->cmap, &xc);
xdev->colors[i] = xc.pixel;
}
}
}
xdev->ht.pixmap = (Pixmap)0;
xdev->ht.bits = 0;
xdev->fill_style = FillSolid;
xswa.event_mask = 0;
xswa.background_pixel = xdev->black;
xdev->win = XCreateWindow(xdev->dpy, RootWindowOfScreen(xdev->scr),
winX, winY, winW, winH, 0,
DefaultDepthOfScreen(xdev->scr), InputOutput,
xdev->vis,
CWEventMask | CWBackPixel, &xswa);
XChangeProperty(xdev->dpy, xdev->win, XA_WM_NAME, XA_STRING, 8,
PropModeReplace, "GhostScript", 11);
XMapWindow(xdev->dpy, xdev->win);
xdev->ht.no_pixmap = XCreatePixmap(xdev->dpy, xdev->win, 1, 1,
DefaultDepthOfScreen(xdev->scr));
/* Set up a graphics context */
xdev->gc = XCreateGC(xdev->dpy, xdev->win, 0, NULL);
XSetFunction(xdev->dpy, xdev->gc, GXcopy);
XSetLineAttributes(xdev->dpy, xdev->gc, 0, LineSolid, CapButt, JoinMiter);
XSync(xdev->dpy, 0);
getchar(); /****** X LOSES OUTPUT OTHERWISE ******/
return 0;
}
/* Close the device. NOT SURE WHAT TO DO HERE YET. */
int
x_close(dev) gx_device *dev;
{ return 0;
}
/* Map a color. The "device colors" are just r,g,b packed together. */
int
x_map_rgb_color(dev, r, g, b) register gx_device *dev; ushort r; ushort g; ushort b;
{ return (r << 2) + (g << 1) + b;
}
/* Map a "device color" back to r-g-b. */
int
x_map_color_rgb(dev, color, prgb) register gx_device *dev; int color; ushort *prgb;
{ prgb[0] = (color >> 2) & 1;
prgb[1] = (color >> 1) & 1;
prgb[2] = color & 1;
}
/* Synchronize the display with the commands already given */
int
x_sync(dev) register gx_device *dev;
{ XSync(xdev->dpy, 0);
return 0;
}
/* Fill a rectangle with a color. */
int
x_fill_rectangle(dev, x, y, w, h, color) register gx_device *dev;
int x; int y; int w; int h; int color;
{ check_rect();
set_fill_style(FillSolid);
set_fore_color(color);
XFillRectangle(xdev->dpy, xdev->win, xdev->gc, x, y, w, h);
#ifdef gs_DEBUG
if ( gs_debug['F'] )
printf("[F] fill (%d,%d):(%d,%d) %d\n",
x, y, w, h, color);
#endif
return 0;
}
/* Tile a rectangle. */
int
x_tile_rectangle(dev, tile, x, y, w, h, zero, one) register gx_device *dev; gx_bitmap *tile;
int x; int y; int w; int h; int zero; int one;
{ check_rect();
set_back_color(zero);
set_fore_color(one);
if ( !set_tile(dev, tile) )
alt_tile_rectangle(dev, tile, x, y, w, h);
else
{ /* Use the tile to fill the rectangle */
XFillRectangle(xdev->dpy, xdev->win, xdev->gc, x, y, w, h);
}
#ifdef gs_DEBUG
if ( gs_debug['F'] )
printf("[F] tile (%d,%d):(%d,%d) %d,%d\n",
x, y, w, h, zero, one);
#endif
return 0;
}
/* Set up with a specified tile. */
/* Return false if we can't do it for some reason. */
int
set_tile(dev, tile) register gx_device *dev; register gx_bitmap *tile;
{
#ifdef gs_DEBUG
if ( gs_debug['T'] )
return 0;
#endif
/* Set up the tile Pixmap */
if ( tile->width != xdev->ht.width || tile->height != xdev->ht.height ||
xdev->ht.pixmap == (Pixmap)0 )
{ if ( xdev->ht.pixmap != (Pixmap)0 )
XFreePixmap(xdev->dpy, xdev->ht.pixmap);
if ( xdev->ht.bits )
{ free(xdev->ht.bits);
xdev->ht.bits = 0;
}
xdev->ht.pixmap = XCreatePixmap(xdev->dpy, xdev->win,
tile->width, tile->height,
DefaultDepthOfScreen(xdev->scr));
if ( xdev->ht.pixmap == (Pixmap)0 )
return 0;
xdev->ht.bits_size = tile->raster * tile->height;
xdev->ht.bits = (byte *)malloc(xdev->ht.bits_size);
if ( xdev->ht.bits == 0 )
{ XFreePixmap(xdev->dpy, xdev->ht.pixmap);
xdev->ht.pixmap = (Pixmap)0;
return 0;
}
xdev->ht.width = tile->width, xdev->ht.height = tile->height;
xdev->ht.raster = tile->raster;
*xdev->ht.bits = ~*tile->data; /* force copying */
}
/* Copy the tile into the Pixmap if needed */
if ( memcmp(xdev->ht.bits, tile->data, xdev->ht.bits_size) )
{ memcpy(xdev->ht.bits, tile->data, xdev->ht.bits_size);
xdev->image.data = (char *)tile->data;
xdev->image.width = tile->width;
xdev->image.height = tile->height;
xdev->image.bytes_per_line = tile->raster;
set_fill_style(FillSolid);
#ifdef gs_DEBUG
if ( gs_debug['H'] )
{ int i;
printf("[H] 0x%x: width=%d height=%d raster=%d\n",
tile->data, tile->width, tile->height, tile->raster);
for ( i = 0; i < tile->raster * tile->height; i++ )
printf(" %02x", tile->data[i]);
printf("\n");
}
#endif
XSetTile(xdev->dpy, xdev->gc, xdev->ht.no_pixmap); /* *** X bug *** */
XPutImage(xdev->dpy, xdev->ht.pixmap, xdev->gc, &xdev->image,
0, 0, 0, 0, tile->width, tile->height);
XSetTile(xdev->dpy, xdev->gc, xdev->ht.pixmap);
}
set_fill_style(FillTiled);
return use_XSetTile;
}
/* Fallback code for tiling when we can't allocate the Pixmap, */
/* or XSetTile/XPutImage doesn't work. */
int
alt_tile_rectangle(dev, tile, x, y, w, h) register gx_device *dev; gx_bitmap *tile;
int x; int y; int w; int h;
{ int tile_width = tile->width, tile_height = tile->height;
int ex = x + w, ey = y + h;
int etx = ex - tile_width, ety = ey - tile_height;
int tx, ty;
int xmod = x % tile_width, ymod = y % tile_height;
int th = tile_height - ymod;
int tw = tile_width - xmod;
if ( tw > w ) tw = w;
if ( th > h ) th = h;
xdev->image.data = (char *)tile->data;
xdev->image.width = tile->width;
xdev->image.height = tile->height;
xdev->image.bytes_per_line = tile->raster;
set_fill_style(FillSolid);
for ( ty = y; ty < ey; )
{ XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image,
xmod, ymod, x, ty, tw, th);
tx = x + tw;
while ( tx <= etx )
{ XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image,
0, ymod, tx, ty, tile_width, th);
tx += tile_width;
}
if ( tx < ex )
XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image,
0, ymod, tx, ty, ex - tx, th);
ty += th;
th = (ty > ety ? ey - ty : tile_height);
ymod = 0;
}
}
/* Copy a monochrome bitmap. */
int
x_copy_mono(dev, base, sourcex, raster, x, y, w, h, zero, one) register gx_device *dev; byte *base; int sourcex; int raster;
int x; int y; int w; int h; int zero; int one;
{ check_rect();
xdev->image.width = raster << 3;
xdev->image.height = h;
xdev->image.data = (char *)base;
xdev->image.bytes_per_line = raster;
/****** HANDLE zero=-1 OR one=-1! ******/
set_back_color(zero);
set_fore_color(one);
set_fill_style(FillSolid);
XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image, sourcex, 0, x, y, w, h);
return 0;
}
/* Copy a "color" bitmap. Since "color" is the same as monochrome, */
/* this just reduces to copying a monochrome bitmap. */
int
x_copy_color(dev, base, sourcex, raster, x, y, w, h, color_transparent) register gx_device *dev; byte *base; int sourcex; int raster;
int x; int y; int w; int h; int color_transparent;
{ int zero = 0, one = 1;
switch ( color_transparent )
{
case 0: zero = -1; break;
case 1: one = -1; break;
}
return x_copy_mono(dev, base, sourcex, raster, x, y, w, h, zero, one);
}
/* Draw a line */
int
x_draw_line(dev, x0, y0, x1, y1, color) register gx_device *dev;
int x0; int y0; int x1; int y1; int color;
{ set_fore_color(color);
XDrawLine(xdev->dpy, xdev->win, xdev->gc, x0, y0, x1, y1);
return 0;
}
/* Fill a trapezoid */
int
x_fill_trapezoid(dev, x0, y0, w0, x1, y1, w1, color) register gx_device *dev;
int x0; int y0; int w0; int x1; int y1; int w1; int color;
{ XPoint vlist[4];
XPoint *pv = vlist + 1;
vlist[0].x = x0, vlist[0].y = y0;
if ( w0 )
pv->x = x0 + w0, pv->y = y0, pv++;
pv->x = x1 + w1, pv->y = y1, pv++;
if ( w1 )
pv->x = x1, pv->y = y1, pv++;
set_fore_color(color);
set_fill_style(FillSolid);
XFillPolygon(xdev->dpy, xdev->win, xdev->gc,
vlist, pv - vlist, Convex, CoordModeOrigin);
return 0;
}
/* ------ Internal procedures ------ */
/* Substitute for XPutImage using XFillRectangle. */
/* This is a total hack to get around an apparent bug */
/* in the X server. It only works with the specific */
/* parameters (bit/byte order, padding) used above. */
static void
alt_put_image(dev, dpy, win, gc, pi, sx, sy, dx, dy, w, h) gx_device *dev; Display *dpy; Drawable win; GC gc;
XImage *pi; int sx; int sy; int dx; int dy; unsigned w; unsigned h;
{ int raster = pi->bytes_per_line;
byte *data = (byte *)pi->data + sy * raster + (sx >> 3);
int init_mask = 0x80 >> (sx & 7);
int invert;
int yi;
#define nrects 40
XRectangle rects[nrects];
XRectangle *rp = rects;
if ( xdev->fore_color >= 0 )
{ if ( xdev->back_color >= 0 )
{ XSetForeground(dpy, gc, xdev->colors[xdev->back_color]);
XFillRectangle(dpy, win, gc, dx, dy, w, h);
}
XSetForeground(dpy, gc, xdev->colors[xdev->fore_color]);
invert = 0;
}
else if ( xdev->back_color >= 0 )
{ XSetForeground(dpy, gc, xdev->colors[xdev->back_color]);
invert = 0xff;
}
else
return;
for ( yi = 0; yi < h; yi++, data += raster )
{ register int mask = init_mask;
register byte *dp = data;
register int xi = 0;
while ( xi < w )
{ if ( (*dp ^ invert) & mask )
{ int xleft = xi;
if ( rp == &rects[nrects] )
{ XFillRectangles(dpy, win, gc, rects, nrects);
rp = rects;
}
/* Scan over a run of 1-bits */
rp->x = dx + xi, rp->y = dy + yi;
do
{ if ( !(mask >>= 1) ) mask = 0x80, dp++;
xi++;
}
while ( xi < w && (*dp & mask) );
rp->width = xi - xleft, rp->height = 1;
rp++;
}
else
{ if ( !(mask >>= 1) ) mask = 0x80, dp++;
xi++;
}
}
}
XFillRectangles(dpy, win, gc, rects, rp - rects);
}